﻿#include "DialogTracer2DSettings.h"
#include "ui_DialogTracer2DSettings.h"

#include <QPushButton>
#include <QLineEdit>
#include <QComboBox>
#include <QCheckBox>
#include <QTableWidgetItem>
#include <QHeaderView>

#include "IdaWidgetHelper.h"

#include "BaseModule/GlobalHelper.h"
#include "StyleComboBox.h"
#include "MainWindowSignalHandler.h"
#include "BaseWidget/DoubleSpinBox.h"
#include "BaseWidget/EColorComboBox.h"
#include "BaseWidget/IdaDialogMsg.h"

namespace pst
{
    DialogTracer2DSettings::DialogTracer2DSettings(QWidget* parent)
        : IdaDialogFrame(parent), ui(new Ui::DialogTracer2DSettings()), m_isHeatmapTracer(false), m_tracerProperty(nullptr), m_tableWidget_tracer(nullptr), m_guideLineProperty(nullptr), m_tableWidget_guideLine(nullptr)
    {
        ui->setupUi(this);

        initWidget();
    }
                
    DialogTracer2DSettings::~DialogTracer2DSettings()
    {
        delete ui;
        //if (m_tracerProperty)
        //{
        //    delete m_tracerProperty;
        //    m_tracerProperty = nullptr;
        //}
        //if (m_guideLineProperty)
        //{
        //    delete m_guideLineProperty;
        //    m_guideLineProperty = nullptr;
        //}
    }

    void DialogTracer2DSettings::setTracerPropertyWidgetRowIndex(int index)
    {
        m_tracerPropertyWidgetRowIndex = index;
    }

    void DialogTracer2DSettings::initWidget()
    {
        setWindowSize(740, 400);
        setWindowTitle(QStringLiteral("显示设置"));

        // auto _button_apply = new QPushButton(QStringLiteral("应用"));
        // ui->buttonBox->addButton(_button_apply, QDialogButtonBox::YesRole);
        connect(ui->pushButton_reject, &QPushButton::clicked, this, &DialogTracer2DSettings::reject);
        connect(ui->pushButton_accept, &QPushButton::clicked, this, &DialogTracer2DSettings::accept);
        connect(ui->pushButton_apply, &QPushButton::clicked, this, &DialogTracer2DSettings::slot_apply);

        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_accept); // 将确定设置为主按钮

        m_tableWidget_tracer = initData_tracer();
        IdaWidgetHelperInstance->setTableWidgetStyle(m_tableWidget_tracer);
        ui->gridLayout_tracer->addWidget(m_tableWidget_tracer);

        //m_tracerProperty = new Style2D_TracerProperty();

        //updateUIData_tracer();

        m_tableWidget_guideLine = initData_guideLine();
        IdaWidgetHelperInstance->setTableWidgetStyle(m_tableWidget_guideLine);
        ui->gridLayout_guideLine->addWidget(m_tableWidget_guideLine);

        //m_guideLineProperty = new Style2D_GuideLineProperty();

        //updateUIData_guideLine();
    }

    void DialogTracer2DSettings::initData(const DataTracer2DSettings dataTracer2DSettings)
    {
        if (dataTracer2DSettings.tracerProperty != nullptr)
        {
            m_tracerProperty = dataTracer2DSettings.tracerProperty;
            //m_tracerProperty->copyData(dataTracer2DSettings.tracerProperty);
            updateUIData_tracer();
        }
        if (dataTracer2DSettings.guideLineProperty != nullptr)
        {
            m_guideLineProperty = dataTracer2DSettings.guideLineProperty;
            //m_guideLineProperty->copyData(dataTracer2DSettings.guideLineProperty);
            updateUIData_guideLine();
        }
    }

    void DialogTracer2DSettings::ChangeVariableText(const QString& variableText)
    {
        auto item = m_tableWidget_tracer->item(1, 0);
        if (item != nullptr)
        {
            item->setText(variableText);
        }
    }

    void DialogTracer2DSettings::EnableHeatmapTracer(bool isHeatmapTracer)
    {
        m_isHeatmapTracer = isHeatmapTracer;
        auto tmp = m_tableWidget_tracer;
        ui->gridLayout_tracer->removeWidget(m_tableWidget_tracer);
        m_tableWidget_tracer = initData_tracer();
        IdaWidgetHelperInstance->setTableWidgetStyle(m_tableWidget_tracer);
        ui->gridLayout_tracer->addWidget(m_tableWidget_tracer);
        updateUIData_tracer();
        if (m_isHeatmapTracer)
        {
            ui->tabWidget->removeTab(1);
        }
        else
        {
            ui->tabWidget->addTab(ui->tab_2, "参考线");
        }
        delete tmp;
    }

    void DialogTracer2DSettings::ChangeCurrentWidget(int index)
    {
        if (index == 0)
        {
            ui->tabWidget->setTabEnabled(0, true);
            if (!m_isHeatmapTracer)
            {
                ui->tabWidget->setTabEnabled(1, false);
            }
        }
        else
        {
            ui->tabWidget->setTabEnabled(0, false);
            if (!m_isHeatmapTracer)
            {
                ui->tabWidget->setTabEnabled(1, true);
            }
        }
        ui->tabWidget->setCurrentIndex(index);
        auto _currentWindowType = MainWindowSignalHandler::getInstance()->getCurrentGraphWindowType();
        if (_currentWindowType == CurrentWindowType::PolarGraph)
        {
            ui->tabWidget->removeTab(1);
        }
    }

    void DialogTracer2DSettings::reject()
    {

        IdaDialogFrame::reject();
    }

    void DialogTracer2DSettings::accept()
    {
        slot_apply();
        if (m_execSuccessFlag)
        {
            IdaDialogFrame::accept();
        }
    }

    void DialogTracer2DSettings::slot_apply()
    {
        m_execSuccessFlag = true;
        if (m_tracerID != -1)
            saveUIData_tracer();
        if (m_execSuccessFlag)
        {
            if (m_guideLineID != -1)
                saveUIData_guideLine();
            //emit signal_sendData({m_tracerProperty, m_guideLineProperty});
            emit signal_sendData(m_tracerID,m_guideLineID);
        }
        
    }

    QTableWidget* DialogTracer2DSettings::initData_tracer()
    {
        auto _tableWidget = new QTableWidget();
        _tableWidget->setColumnCount(2);
        if (m_isHeatmapTracer)
        {
            _tableWidget->setRowCount(6);
        }
        else
        {
            _tableWidget->setRowCount(5);
        }
        _tableWidget->setHorizontalHeaderLabels(QStringLiteral("名称,值").split(","));
        _tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        auto _lineWidthList = GlobalHelperInstance->getLineWidthList();

        int _rowCount = -1;
        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("标记名称")));
        auto _lineEdit_tracerName = new QLineEdit; // 标记名称
        _tableWidget->setCellWidget(_rowCount, 1, _lineEdit_tracerName);

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("x")));
        auto _doubleSpinBox_xValue = new BaseWidget::DoubleSpinBox(); // x
        _tableWidget->setCellWidget(_rowCount, 1, _doubleSpinBox_xValue);

        if (m_isHeatmapTracer)
        {
            _doubleSpinBox_xValue->setEnabled(false);
            _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("y")));
            auto _doubleSpinBox_yValue = new BaseWidget::DoubleSpinBox(); // y
            _tableWidget->setCellWidget(_rowCount, 1, _doubleSpinBox_yValue);
            _doubleSpinBox_yValue->setEnabled(false);
        }

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("符号样式")));
        auto _comboBox_symbolStyle = new TracerStyleComboBox; // 符号样式
        _tableWidget->setCellWidget(_rowCount, 1, _comboBox_symbolStyle);

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("符号大小")));
        auto _comboBox_symbolSize = new QComboBox; // 符号大小
        _tableWidget->setCellWidget(_rowCount, 1, _comboBox_symbolSize);
        for (int i = 0; i < _lineWidthList.size() - 4; ++i)
        {
            _comboBox_symbolSize->addItem(_lineWidthList.at(i));
        }

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("符号颜色")));
        auto _comboBox_symbolColor = new BaseWidget::EColorComboBox; // 符号颜色
        _comboBox_symbolColor->appendPredefinedColors();
        _tableWidget->setCellWidget(_rowCount, 1, _comboBox_symbolColor);

        // 设置第一列不可编辑
        for (int _row = 0; _row < _tableWidget->rowCount(); ++_row)
        {
            QTableWidgetItem* item = _tableWidget->item(_row, 0);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        }

        return _tableWidget;
    }

    QTableWidget* DialogTracer2DSettings::initData_guideLine()
    {
        auto _tableWidget = new QTableWidget();
        _tableWidget->setColumnCount(2);
        _tableWidget->setRowCount(4);
        _tableWidget->setHorizontalHeaderLabels(QStringLiteral("名称,值").split(","));
        _tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

        auto _lineWidthList = GlobalHelperInstance->getLineWidthList();

        int _rowCount = -1;
        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("参考线值")));
        auto _doubleSpinBox_lineValue = new BaseWidget::DoubleSpinBox(); // 参考线值
        _doubleSpinBox_lineValue->setRange(-1e15, 1e15);
        _doubleSpinBox_lineValue->setDecimals(15);
        _tableWidget->setCellWidget(_rowCount, 1, _doubleSpinBox_lineValue);

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("参考线样式")));
        auto _comboBox_lineStyle = new LineStyleComboBox; // 参考线样式
        _tableWidget->setCellWidget(_rowCount, 1, _comboBox_lineStyle);

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("参考线粗细")));
        auto _comboBox_lineThickness = new QComboBox(); // 参考线粗细
        _tableWidget->setCellWidget(_rowCount, 1, _comboBox_lineThickness);
        for (int i = 0; i < _lineWidthList.size(); ++i)
        {
            _comboBox_lineThickness->addItem(_lineWidthList.at(i));
        }

        _tableWidget->setItem(++_rowCount, 0, new QTableWidgetItem(QStringLiteral("参考线颜色")));
        auto _comboBox_lineColor = new BaseWidget::EColorComboBox; // 参考线颜色
        _comboBox_lineColor->appendPredefinedColors();
        _tableWidget->setCellWidget(_rowCount, 1, _comboBox_lineColor);

        // 设置第一列不可编辑
        for (int _row = 0; _row < _tableWidget->rowCount(); ++_row)
        {
            QTableWidgetItem* item = _tableWidget->item(_row, 0);
            item->setFlags(item->flags() & ~Qt::ItemIsEditable);
        }

        return _tableWidget;
    }

    void DialogTracer2DSettings::saveUIData_tracer()
    {
        auto _tableWidget = m_tableWidget_tracer;
        auto _tracerProperty = m_tracerProperty;

        int _rowCount = -1;
        auto _lineEdit_tracerName = (QLineEdit*)_tableWidget->cellWidget(++_rowCount, 1);  // 标记名称
        for (auto it = m_existMarkerMap.cbegin(); it != m_existMarkerMap.cend(); ++it)
        {
            int index = it.key();
            if (index != m_currentMarkerIndex)
            {
                if (_lineEdit_tracerName->text() == m_existMarkerMap.value(index))
                {
                    m_execSuccessFlag = false;
                    IdaDialogMsg::showDialog(this, QString("名称已存在，请重新填写。"), EDialogMsg::Error, tr("重命名错误"));
                    return;
                }
            }
        }

        auto _doubleSpinBox_xValue = (BaseWidget::DoubleSpinBox*)_tableWidget->cellWidget(++_rowCount, 1); // x
        BaseWidget::DoubleSpinBox* _doubleSpinBox_yValue = nullptr;
        if (m_isHeatmapTracer)
        {
            _doubleSpinBox_yValue = (BaseWidget::DoubleSpinBox*)_tableWidget->cellWidget(++_rowCount, 1); // y
        }
        auto _comboBox_symbolStyle = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                  // 符号样式
        auto _comboBox_symbolSize = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                   // 符号大小
        auto _comboBox_symbolColor = (BaseWidget::EColorComboBox*)_tableWidget->cellWidget(++_rowCount, 1); // 符号颜色

        _tracerProperty->m_tracerName = _lineEdit_tracerName->text(); // 结果数据名称
        _tracerProperty->m_x = _doubleSpinBox_xValue->value();
        if (m_isHeatmapTracer)
        {
            _tracerProperty->m_y = _doubleSpinBox_yValue->value();
        }
        _tracerProperty->m_symbolStyle = _comboBox_symbolStyle->currentIndex();         // 符号样式
        _tracerProperty->m_symbolSize = _comboBox_symbolSize->currentText().toDouble(); // 符号大小
        _tracerProperty->m_symbolColor = _comboBox_symbolColor->currentColor();         // 符号颜色
    }

    void DialogTracer2DSettings::saveUIData_guideLine()
    {
        auto _tableWidget = m_tableWidget_guideLine;
        auto _guideLineProperty = m_guideLineProperty;

        int _rowCount = -1;
        auto _doubleSpinBox_lineValue = (BaseWidget::DoubleSpinBox*)_tableWidget->cellWidget(++_rowCount, 1); // 参考线值
        auto _comboBox_lineStyle = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                      // 参考线样式
        auto _comboBox_lineThickness = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                  // 参考线粗细
        auto _comboBox_lineColor = (BaseWidget::EColorComboBox*)_tableWidget->cellWidget(++_rowCount, 1);     // 参考线颜色

        _guideLineProperty->m_value = _doubleSpinBox_lineValue->value();                              // 参考线值
        _guideLineProperty->m_guideLineStyle = _comboBox_lineStyle->currentIndex();                   // 参考线样式
        _guideLineProperty->m_guideLineThickness = _comboBox_lineThickness->currentText().toDouble(); // 参考线粗细
        _guideLineProperty->m_guideLineColor = _comboBox_lineColor->currentColor();                   // 参考线颜色
    }

    void DialogTracer2DSettings::updateUIData_tracer()
    {
        auto _tableWidget = m_tableWidget_tracer;
        auto _tracerProperty = m_tracerProperty;

        int _rowCount = -1;
        auto _lineEdit_tracerName = (QLineEdit*)_tableWidget->cellWidget(++_rowCount, 1);                  // 标记名称
        auto _doubleSpinBox_xValue = (BaseWidget::DoubleSpinBox*)_tableWidget->cellWidget(++_rowCount, 1); // x
        BaseWidget::DoubleSpinBox* _doubleSpinBox_yValue = nullptr;
        if (m_isHeatmapTracer)
        {
            _doubleSpinBox_yValue = (BaseWidget::DoubleSpinBox*)_tableWidget->cellWidget(++_rowCount, 1); // y
        }
        auto _comboBox_symbolStyle = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                  // 符号样式
        auto _comboBox_symbolSize = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                   // 符号大小
        auto _comboBox_symbolColor = (BaseWidget::EColorComboBox*)_tableWidget->cellWidget(++_rowCount, 1); // 符号颜色

        _lineEdit_tracerName->setText(_tracerProperty->m_tracerName); // 结果数据名称
        _doubleSpinBox_xValue->setValue(_tracerProperty->m_x);
        if (m_isHeatmapTracer)
        {
            _doubleSpinBox_yValue->setValue(_tracerProperty->m_y);
        }
        _comboBox_symbolStyle->setCurrentIndex(_tracerProperty->m_symbolStyle);               // 符号样式
        _comboBox_symbolSize->setCurrentText(QString::number(_tracerProperty->m_symbolSize)); // 符号大小
        _comboBox_symbolColor->setCurrentColor(_tracerProperty->m_symbolColor);               // 符号颜色
    }

    void DialogTracer2DSettings::updateUIData_guideLine()
    {
        auto _tableWidget = m_tableWidget_guideLine;
        auto _guideLineProperty = m_guideLineProperty;

        int _rowCount = -1;
        auto _doubleSpinBox_lineValue = (BaseWidget::DoubleSpinBox*)_tableWidget->cellWidget(++_rowCount, 1); // 参考线值
        auto _comboBox_lineStyle = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                      // 参考线样式
        auto _comboBox_lineThickness = (QComboBox*)_tableWidget->cellWidget(++_rowCount, 1);                  // 参考线粗细
        auto _comboBox_lineColor = (BaseWidget::EColorComboBox*)_tableWidget->cellWidget(++_rowCount, 1);     // 参考线颜色

        _doubleSpinBox_lineValue->setValue(_guideLineProperty->m_value);                                    // 参考线值
        _comboBox_lineStyle->setCurrentIndex(_guideLineProperty->m_guideLineStyle);                         // 参考线样式
        _comboBox_lineThickness->setCurrentText(QString::number(_guideLineProperty->m_guideLineThickness)); // 参考线粗细
        _comboBox_lineColor->setCurrentColor(_guideLineProperty->m_guideLineColor);                         // 参考线颜色
    }

    void DialogTracer2DSettings::setCurrentMarkerIndex(int index)
    {
        m_currentMarkerIndex = index;
    }
    void DialogTracer2DSettings::setExistMarkerMap(QHash<int, QString> map)
    {
        m_existMarkerMap = map;
    }

} // namespace pst
